package com.ideasmaker.dao.idea.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ideasmaker.bean.Idea;
import com.ideasmaker.dao.idea.IIdeaDao;
import com.ideasmaker.util.HibernateSessionUtil;

public class IdeaDaoImpl extends HibernateDaoSupport implements IIdeaDao {

	public void saveIdea(Idea idea) throws SQLException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(idea);

	}

	public void deleteIdea(Idea idea) throws SQLException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(idea);

	}

	public void updateIdea(Idea idea) throws SQLException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(idea);

	}

	public Idea queryIdeaById(Idea idea) throws SQLException {
		// TODO Auto-generated method stub
		Session session = HibernateSessionUtil.getSession();
		Criteria criteria = session.createCriteria(Idea.class);
		return (Idea)criteria.add(Restrictions.eq("id", idea.getId()));
	}

	@SuppressWarnings("unchecked")
	public List<Idea> queryIdeaByFilter(Idea idea) throws SQLException {
		// TODO Auto-generated method stub
		Session session = HibernateSessionUtil.getSession();
		Criteria criteria = session.createCriteria(Idea.class);
		criteria.add(Restrictions.like("title", idea.getTitle(), MatchMode.ANYWHERE));
		criteria.add(Restrictions.like("label", idea.getLabel(),MatchMode.ANYWHERE));
		List<Idea> list = criteria.list();
		return list;
	}

}
